import { createRouter, createWebHistory } from 'vue-router';
import CarDetail from '@/views/car/CarDetail.vue'
import { isLoggedIn } from '@/utils/auth'
import { ElMessage } from 'element-plus'

/* Layouts */
import AdminLayout from '@/layout/index.vue';
import FrontLayout from '@/layout/FrontLayout.vue';

/**
 * 前台路由
 */
export const frontendRoutes = [
  {
    path: '/',
    component: FrontLayout,
    children: [
      {
        path: '',
        component: () => import('@/views/front/home/index.vue'),
        name: 'Home',
        meta: { title: '首页' }
      },
      {
        path: 'cars',
        component: () => import('@/views/front/cars/index.vue'),
        name: 'Cars',
        meta: { title: '车辆展示' }
      },
      {
        path: 'car/:id',
        component: CarDetail,
        name: 'CarDetail',
        meta: { title: '车辆详情' }
      },
      {
        path: 'dealers',
        component: () => import('@/views/dealer/DealerList.vue'),
        name: 'DealerList',
        meta: { title: '经销商列表' }
      },
      {
        path: 'my-transactions',
        component: () => import('@/views/transaction/user/index.vue'),
        name: 'UserTransactions',
        meta: { title: '我的交易', requiresAuth: true }
      },
      {
        path: 'user',
        redirect: '/user/profile',
        meta: { requiresAuth: true }
      },
      {
        path: 'user/profile',
        component: () => import('@/views/front/user/profile/index.vue'),
        name: 'UserProfile',
        meta: { title: '个人中心', requiresAuth: true }
      },
      {
        path: 'user/orders',
        component: () => import('@/views/front/user/orders/index.vue'),
        name: 'UserOrders',
        meta: { title: '我的订单', requiresAuth: true }
      },
      {
        path: 'user/favorites',
        component: () => import('@/views/front/user/favorites/index.vue'),
        name: 'UserFavorites',
        meta: { title: '我的收藏', requiresAuth: true }
      },
      {
        path: 'about',
        component: () => import('@/views/front/about/index.vue'),
        name: 'About',
        meta: { title: '关于我们' }
      },
      {
        path: 'contact',
        component: () => import('@/views/front/contact/index.vue'),
        name: 'Contact',
        meta: { title: '语音识别' }
      },
      {
        path: 'metaverse',
        component: () => import('@/views/front/metaverse/index.vue'),
        name: 'Metaverse',
        meta: { title: '二手车元宇宙' }
      },
      {
        path: 'digital-human',
        component: () => import('@/views/front/digital-human/index.vue'),
        name: 'DigitalHuman',
        meta: { title: '数字人功能' }
      }
    ]
  }
];

/**
 * 后台管理路由
 */
export const adminRoutes = [
  {
    path: '/admin/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  {
    path: '/admin/register',
    component: () => import('@/views/register/index.vue'),
    hidden: true
  },
  {
    path: '/admin',
    component: AdminLayout,
    redirect: '/admin/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'AdminDashboard',
        meta: { title: '首页', icon: 'Monitor', affix: true }
      }
    ]
  },
  {
    path: '/admin/profile',
    component: AdminLayout,
    redirect: '/admin/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index.vue'),
        name: 'AdminProfile',
        meta: { title: '个人中心', icon: 'User' }
      }
    ]
  },
  
  // 系统管理
  {
    path: '/admin/system',
    component: AdminLayout,
    redirect: '/admin/system/user',
    name: 'System',
    meta: { title: '系统管理', icon: 'Setting' },
    children: [
      {
        path: 'user',
        component: () => import('@/views/system/user/index.vue'),
        name: 'User',
        meta: { title: '用户管理', icon: 'User' }
      },
      {
        path: 'employee',
        component: () => import('@/views/system/employee/index.vue'),
        name: 'Employee',
        meta: { title: '员工管理', icon: 'User' }
      },
      {
        path: 'role',
        component: () => import('@/views/system/role/index.vue'),
        name: 'Role',
        meta: { title: '角色管理', icon: 'User' }
      },
      {
        path: 'menu',
        component: () => import('@/views/system/menu/index.vue'),
        name: 'Menu',
        meta: { title: '菜单管理', icon: 'Menu' }
      }
    ]
  },
  
  // 车辆管理
  {
    path: '/admin/car',
    component: AdminLayout,
    redirect: '/admin/car/list',
    name: 'Car',
    meta: { title: '车辆管理', icon: 'Van' },
    children: [
      {
        path: 'list',
        component: () => import('@/views/car/list/index.vue'),
        name: 'CarList',
        meta: { title: '车辆列表', icon: 'List' }
      },
      {
        path: 'inspection',
        component: () => import('@/views/car/inspection/index.vue'),
        name: 'Inspection',
        meta: { title: '车辆检查', icon: 'View' }
      },
      {
        path: 'comment',
        component: () => import('@/views/car/comment/index.vue'),
        name: 'Comment',
        meta: { title: '车辆评论', icon: 'ChatLineRound' }
      },
      {
        path: 'photo',
        component: () => import('@/views/car/photo/index.vue'),
        name: 'Photo',
        meta: { title: '车辆图片', icon: 'Picture' }
      },
      {
        path: 'axios-demo',
        component: () => import('@/views/car/axios-demo.vue'),
        name: 'AxiosDemo',
        meta: { title: 'Axios演示', icon: 'Connection' }
      },
      {
        path: 'owner',
        component: () => import('@/views/car/owner/index.vue'),
        name: 'CarOwner',
        meta: { title: '车主管理', icon: 'User' }
      },
      {
        path: 'photo/:id',
        name: 'CarPhoto',
        component: () => import('@/views/car/photo/index.vue'),
        meta: { title: '车辆图片', activeMenu: '/car/list' },
        hidden: true
      }
    ]
  },
  
  // 交易管理
  {
    path: '/admin/transaction',
    component: AdminLayout,
    redirect: '/admin/transaction/appointment',
    name: 'Transaction',
    meta: { title: '交易管理', icon: 'Money' },
    children: [
      {
        path: 'appointment',
        component: () => import('@/views/appointment/index.vue'),
        name: 'Appointment',
        meta: { title: '预约管理', icon: 'Calendar' }
      },
      {
        path: 'record',
        component: () => import('@/views/transaction/record/index.vue'),
        name: 'Record',
        meta: { title: '交易记录', icon: 'Document' }
      },
      {
        path: 'favorite',
        component: () => import('@/views/transaction/favorite/index.vue'),
        name: 'Favorite',
        meta: { title: '收藏管理', icon: 'Star' }
      }
    ]
  },
  
  // 经销商管理
  {
    path: '/admin/dealer',
    component: AdminLayout,
    redirect: '/admin/dealer/list',
    name: 'Dealer',
    meta: { title: '经销商管理', icon: 'Shop' },
    children: [
      {
        path: 'list',
        component: () => import('@/views/dealer/list/index.vue'),
        name: 'DealerList',
        meta: { title: '经销商列表', icon: 'List' }
      }
    ]
  },
  
  // 客服管理
  {
    path: '/admin/customer-service',
    component: AdminLayout,
    redirect: '/admin/customer-service/index',
    name: 'CustomerServiceManagement',
    meta: { title: '客服管理', icon: 'ChatLineSquare' },
    children: [
      {
        path: 'index',
        component: () => import('@/views/customer-service/index.vue'),
        name: 'CustomerService',
        meta: { title: '在线客服', icon: 'Service' }
      }
    ]
  },
  {
    path: '/voice',
    component: AdminLayout,
    children: [
      {
        path: 'to-text',
        name: 'VoiceToText',
        component: () => import('@/views/voice/VoiceToText.vue'),
        meta: { title: '语音转文字', icon: 'Microphone' }
      }
    ]
  }
];

/**
 * 错误页和通用路由
 */
export const commonRoutes = [
  // 登录和注册路由（前台用户）
  {
    path: '/login',
    component: () => import('@/views/front/login/index.vue'),
    name: 'Login',
    meta: { title: '登录' }
  },
  {
    path: '/register',
    component: () => import('@/views/front/register/index.vue'),
    name: 'Register',
    meta: { title: '注册' }
  },
  {
    path: '/forget-password',
    component: () => import('@/views/front/forget-password/index.vue'),
    name: 'ForgetPassword',
    meta: { title: '忘记密码' }
  },
  
  // 重定向路由 - 当访问 /car/list 时重定向到 /admin/car/list
  {
    path: '/car/list',
    redirect: '/admin/car/list'
  },
  
  // 404 页面
  {
    path: '/404',
    component: () => import('@/views/error/404.vue'),
    hidden: true
  },
  
  // 匹配所有未定义的路由，必须放在最后
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    hidden: true
  }
];

// 合并所有路由
const routes = [...frontendRoutes, ...adminRoutes, ...commonRoutes];

const router = createRouter({
  history: createWebHistory(),
  routes: routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - CarShop二手车`;
  }
  
  // 检查是否需要登录
  if (to.meta.requiresAuth && !isLoggedIn()) {
    ElMessage.warning('请先登录')
    next({
      path: '/login',
      query: { redirect: to.fullPath }
    })
    return
  }
  
  // Handle admin dashboard specially
  if (to.path === '/admin/dashboard') {
    console.log('Navigating to admin dashboard');
  }
  
  // Always allow navigation
  next();
});

export default router; 